package com.zzz.service.impl;

import com.aaa.entity.OOrder;
import com.aaa.entity.OPayLog;
import com.aaa.entity.OStock;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.util.PayUtils;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzz.feign.AliPayFeign;
import com.zzz.feign.RabbitmqFeigen;
import com.zzz.feign.WxFeign;
import com.zzz.mapper.OPayLogMapper;
import com.zzz.mapper.OStockMapper;
import com.zzz.service.OOrderService;
import com.zzz.mapper.OOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* @author 86176
* @description 针对表【o_order(订单信息)】的数据库操作Service实现
* @createDate 2023-03-09 10:32:49
*/
@Service
@SuppressWarnings("all")
public class OOrderServiceImpl extends ServiceImpl<OOrderMapper, OOrder>
    implements OOrderService{

    @Autowired
    private OOrderMapper oOrderMapper;

    @Autowired
    private AliPayFeign ailPayFeign;

    @Autowired
    private WxFeign wxFeign;

    @Autowired
    private OPayLogMapper oPayLogMapper;

    @Autowired
    private OStockMapper oStockMapper;

    @Autowired
    private RabbitmqFeigen rabbitmqFeigen;


    //页面加载判断该用户是否买过该商品
    @Override
    public Result hasBuyThisGoods(String goodsid) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        Object username1 = tokenChaim.get("username");
        String username = username1.toString();
        List<OOrder> oOrders = oOrderMapper.hasBuyThisGoods(goodsid, username);
        if (oOrders == null || oOrders.size() == 0 ) {
            return new Result(5000, "查询失败",oOrders);
        }else {
            return new Result(2000, "查询成功",oOrders);
        }
    }

    //生成支付宝二维码加支付宝支付
    @Override
    public Result getPayInfo(PayUtils payUtils) {
        Result payInfo = ailPayFeign.getPayInfo(payUtils);
        return new Result(2000,"查询成功",payInfo);
    }

    //生成微信二维码加微信支付
    @Override
    public Result buyNow(PayUtils payUtils) {
        Result result = wxFeign.buyNow(payUtils);
        return new Result(2000,"查询成功",result);
    }

    //生成订单
    @Override
    public Result settlement(OOrder oOrder) {
        System.out.println(oOrder+"===================");
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);
        String id = (String) tokenChaim.get("id");
        String s = UUID.randomUUID().toString();
        String[] idd=s.toString().split("-");
        oOrder.setUid(id);
        oOrder.setCode(idd[0]);
        oOrder.setOrderdate(new Date());
        oOrder.setSend(0);
        oOrder.setState(1);
        oOrder.setCommentstatus(0);
        oOrder.setPaysort(1);
        int insert = oOrderMapper.insert(oOrder);
        OPayLog oPayLog = new OPayLog();
        oPayLog.setOrderNo(idd[0]);
        oPayLog.setPayType(1);
        oPayLog.setIsDeleted(0);
        oPayLog.setGmtCreated(new Date());
        oPayLog.setGmtModified(new Date());
        oPayLog.setPaysort(1);
        BigDecimal bigDecimal = new BigDecimal(oOrder.getTotalmoney());
        oPayLog.setTotalFee(bigDecimal);
        int insert1 = oPayLogMapper.insert(oPayLog);
        String goodsid = oOrder.getGoodsid();
        Integer num = oOrder.getNum();
        oStockMapper.updateStock(Integer.valueOf(goodsid),num);
        rabbitmqFeigen.makeChargeOrder(idd[0]);
        return new Result(2000,"添加成功",idd[0]);
    }
}




